import { Component } from '@angular/core';
import { Http } from '@angular/http';
//Base components
import { SubIndustryModel } from '../../../shared/templates/subIndustry/subIndustry.model';
//Services
import { CompanyRiskService } from '../../companyRisk.service';
import { UtilsService } from '../../../shared/services/utils.service';

import * as _ from 'lodash';

@Component({
    selector: 'subindustry-risk-rating',
    templateUrl: '../../../shared/templates/subIndustry/subIndustry.component.html',
    styleUrls: ['../../../shared/templates/subIndustry/subIndustry.component.css']
})

export class SubIndustrySummaryComponent extends SubIndustryModel {
    constructor(
        private riskService: CompanyRiskService,
        private http: Http) {
        super(http);
        this.primaryName = "Universe";
        this.secondaryName = "Subindustry";
    }

    ngOnInit() {
        this.displayRatingDistribution = true;
        this.loaded = new Promise<void>((resolve, reject) => {
            this.riskService.company.then(company => {
                this.currentCompany = company.id;
                this.currentCompanyName = company.companyName;
                this.companyClusterCode = company.assessmentCode;
                this.getCompanyClusters().then(clusters => {
                    this.riskClusters = clusters;
                    this.maxExposure = 100;

                    var universePromise = this.riskService.getCompanyUniverseBenchmark(company.id);
                    var subIndustryPromise = this.riskService.getCompanySubIndustryBenchmark(company.id);
                    var exposurePromise = this.riskService.getCompanySubIndustryExposure(company.id);

                    Promise.all([universePromise, subIndustryPromise, exposurePromise]).then(data => {
                        this.universe = data[0] as any;
                        this.subIndustry = data[1] as any;
                        this.scatter = data[2] as any[];

                        //benchmark chart data
                        let benchmarkResult: any[] = []
                        let maxLimit: number = Math.min(this.universe.chartData.length, this.riskClusters[this.riskClusters.length - 1].topInterval + 1);
                        for (var i = 0; i < maxLimit; i++) {
                            benchmarkResult.push({
                                score: this.universe.chartData[i].xValue,
                                primary: this.universe.chartData[i].yValue,
                                secondary: this.subIndustry.chartData[i].yValue,
                                companyBullet: this.subIndustry.chartData[i].xValue == Math.floor(company.riskRating)
                            });
                        }
                        this.benchmarkData = benchmarkResult;

                        // prepare data
                        let universeData = UtilsService.aggregateDataByRiskCluster(this.riskClusters, this.universe.chartData, 'universe');
                        let subIndustryData = UtilsService.aggregateDataByRiskCluster(this.riskClusters, this.subIndustry.chartData, 'subIndustry');

                        // merge data sets by riskCluster
                        this.benchmarkData = _(universeData)
                            .concat(subIndustryData)
                            .groupBy('riskCluster')
                            .map(_.spread(_.merge))
                            .value();

                        //scatter chart data
                        let companyBullet: any;
                        let scatterResult: any[] = [];
                        this.scatter.forEach(subGroupCompany => {
                            companyBullet = {
                                management: subGroupCompany.management,
                                exposure: subGroupCompany.exposure,
                                color: this.clusterMetadata[subGroupCompany.cluster].color
                            };

                            this.subIndustry.peersList.forEach(peer => {
                                if (peer.companyId == subGroupCompany.companyId) {
                                    companyBullet.peerPosition = peer.rank;
                                    if (subGroupCompany.companyId == company.id) {
                                        companyBullet.currentX = companyBullet.exposure;
                                        companyBullet.currentY = companyBullet.management;
                                    }
                                    else {
                                        companyBullet.peerX = companyBullet.exposure;
                                        companyBullet.peerY = companyBullet.management;
                                    }
                                    return;
                                }
                            });
                            scatterResult.push(companyBullet);
                        });
                        this.exposureData = scatterResult;
                        resolve();
                    });
                });
            });
        });
    }
}